package com.example;
import javax.swing.*;
import java.awt.*;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class MathProblemViewer extends JFrame {
    private JTextArea problemArea;
    private JButton prevButton;
    private JButton nextButton;
    private JLabel pageLabel;
    private List<String> problems;
    private int currentIndex = 0;

    public MathProblemViewer() {
        setTitle("数学口算题");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setSize(400, 300);

        // 初始化题目列表
        problems = new ArrayList<>();

        // 创建界面组件
        problemArea = new JTextArea();
        problemArea.setEditable(false);
        problemArea.setFont(new Font("Microsoft YaHei", Font.PLAIN, 24));
        problemArea.setMargin(new Insets(20, 20, 20, 20));

        // 创建按钮和页码标签
        prevButton = new JButton("上一题");
        nextButton = new JButton("下一题");
        pageLabel = new JLabel("0/0");

        // 创建按钮面板
        JPanel buttonPanel = new JPanel();
        buttonPanel.add(prevButton);
        buttonPanel.add(pageLabel);
        buttonPanel.add(nextButton);

        // 设置布局
        setLayout(new BorderLayout());
        add(new JScrollPane(problemArea), BorderLayout.CENTER);
        add(buttonPanel, BorderLayout.SOUTH);

        // 添加按钮事件
        prevButton.addActionListener(e -> showPreviousProblem());
        nextButton.addActionListener(e -> showNextProblem());

        // 加载题目
        loadProblems("math_problems.csv");

        // 显示第一题
        if (!problems.isEmpty()) {
            showProblem(0);
        }

        // 更新按钮状态
        updateButtonStates();
    }

    private void loadProblems(String fileName) {
        try (BufferedReader reader = new BufferedReader(new FileReader(fileName))) {
            // 跳过标题行
            reader.readLine();
            String line;

            while ((line = reader.readLine()) != null) {
                String[] parts = line.split(",");
                if (parts.length >= 2) {
                    problems.add(String.format("第%s题:\n\n%s", parts[0], parts[1]));
                }
            }

        } catch (IOException e) {
            e.printStackTrace();
            JOptionPane.showMessageDialog(this, "无法读取题目文件！", "错误", JOptionPane.ERROR_MESSAGE);
        }
    }

    private void showProblem(int index) {
        if (index >= 0 && index < problems.size()) {
            problemArea.setText(problems.get(index));
            currentIndex = index;
            pageLabel.setText(String.format("%d/%d", currentIndex + 1, problems.size()));
            updateButtonStates();
        }
    }

    private void showNextProblem() {
        if (currentIndex < problems.size() - 1) {
            showProblem(currentIndex + 1);
        }
    }

    private void showPreviousProblem() {
        if (currentIndex > 0) {
            showProblem(currentIndex - 1);
        }
    }

    private void updateButtonStates() {
        prevButton.setEnabled(currentIndex > 0);
        nextButton.setEnabled(currentIndex < problems.size() - 1);
    }

    public static void main(String[] args) {
        // 首先生成题目
        MathProblemGenerator.generateProblems("math_problems.csv");

        // 然后显示GUI
        SwingUtilities.invokeLater(() -> {
            MathProblemViewer viewer = new MathProblemViewer();
            viewer.setLocationRelativeTo(null);
            viewer.setVisible(true);
        });
    }
}